package com.test.mooan.labama.adapter_boom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.mooan.labama.R;
import com.test.mooan.labama.bean_boom.config_info_boom.AppShowInfo_boom;
import com.test.mooan.labama.utils_boom.Threadutils_boom;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class WltActTypeListAdapter_Fr_boom extends BaseAdapter {

    private final LayoutInflater inflater;
    private final ArrayList<AppShowInfo_boom.WalletChannel> walletChannels;

    public WltActTypeListAdapter_Fr_boom(Context context) {
        inflater = LayoutInflater.from(context);
        this.walletChannels = AppShowInfo_boom.getInstance().gWalletChannels();
    }

    @Override
    public int getCount() {
        return walletChannels.size();
    }

    @Override
    public AppShowInfo_boom.WalletChannel getItem(int i) {
        return walletChannels.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        view = inflater.inflate(R.layout.item_wlt_act_type_fr_boom,viewGroup,false);

        AppShowInfo_boom.WalletChannel walletChannel = walletChannels.get(i);

        ImageView accountTypeIv = view.findViewById(R.id.item_wallet_account_type_iv_account_type);
        TextView accountTypeTv = view.findViewById(R.id.item_wallet_account_type_tv_account_type);

        accountTypeTv.setText(walletChannel.gDesc());

        //获取图标
        if (walletChannel.gBitmap()!=null){
            accountTypeIv.setImageBitmap(walletChannel.gBitmap());
        }else {
            Threadutils_boom.getInstance().runOnChildThread(() -> {
                try {
                    if (TextUtils.isEmpty(walletChannel.gLogo())) return;
                    URL url = new URL(walletChannel.gLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        walletChannel.sBitmap(bitmap);
                        Threadutils_boom.getInstance().runOnUiThread(() -> {
                            if (accountTypeIv!=null) accountTypeIv.setImageBitmap(bitmap);
                        });
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }

        return view;
    }

}
